﻿using IPC.Communication;
using IPC.Communication.Core.Types;
using IPC.DbMapper.IPCDBConnect;
using IPC.ListConnect.Profinet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace IPC.ListConnect.Connect
{
    public class BaseConnect
    {
        #region 字段
        private string _connectName;
        private bool _isOpen = false;
        #endregion

        #region 属性
        public string ConnectName
        {
            get
            {
                return _connectName;
            }
            set
            {
                _connectName = value;
            }
        }

        /// <summary>
        /// 连接能否打开
        /// </summary>
        public bool IsOpen
        {
            get { return _isOpen; }
            set { _isOpen = value; }
        }
        #endregion

        #region 构造函数
        public BaseConnect(string connectName, string ip, int port, int slot, int rack, SiemensVersion siemensVersion = SiemensVersion.S7_1500)
        {
            this.ConnectName = connectName;
        }
        #endregion

        #region 读

        /// <summary>
        /// 读
        /// </summary>
        /// <returns></returns>
        public virtual OperateResult Read()
        {
             return new OperateResult() { IsSuccess=true}; 
        }

        /// <summary>
        /// 按组读
        /// </summary>
        /// <param name="equipChannel">通道名</param>
        /// <param name="equip">设备名</param>
        /// <param name="group">组名</param>
        public virtual OperateResult Read(string channelName, string equip,  string group = "")
        {
            return new OperateResult();
        }

        /// <summary>
        /// 单点读
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <param name="equipName">设备名</param>
        /// <param name="tagName">标签名</param>
        /// <param name="groupName">组名</param>
        public virtual OperateResult Read(string channelName, string equipName,string tagName,string groupName = "")
        {
            return new OperateResult();
        }

        /// <summary>
        /// 按组读 返回json
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equip"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        public virtual string ReadJson(string channelName, string equip, string group = "")
        {
            return "";
        }

        /// <summary>
        /// 单点读 返回json
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="tagName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public virtual string ReadJson(string channelName,  string equipName, string tagName, string groupName = "")
        {
            return "";
        }

        #endregion

        #region 写
        /// <summary>
        /// 多标签写
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <param name="equipName">设备名</param>
        /// <param name="values">值</param>
        /// <param name="startTag">开始点</param>
        /// <param name="groupName">组名</param>
        /// <returns></returns>
        public virtual OperateResult Write(string channelName, string equipName, string[] values,string startTag = "",string groupName = "")
        {
            return new OperateResult();
        }

        /// <summary>
        /// 单标签写
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <param name="equipName">设备名</param>
        /// <param name="tagName">标签名</param>
        /// <param name="values">值</param>
        /// <param name="groupName">组名</param>
        /// <returns></returns>
        public virtual OperateResult Write(string channelName, string equipName, string tagName, string values, string groupName = "")
        {
            return new OperateResult();
        }

        /// <summary>
        /// 多标签写 返回json
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <param name="equipName">设备名</param>
        /// <param name="values">值</param>
        /// <param name="startTag">开始点</param>
        /// <param name="groupName">组名</param>
        /// <returns></returns>
        public virtual string WriteJson(string channelName, string equipName, string[] values, string startTag = "", string groupName = "")
        {
            return "";
        }

        /// <summary>
        /// 多标签写 返回json
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <param name="equipName">设备名</param>
        /// <param name="json">值的json格式 格式为["test1","test2"]</param>
        /// <param name="startTag">开始点</param>
        /// <param name="groupName">组名</param>
        /// <returns></returns>
        public virtual string WriteByJson(string channelName, string equipName, string json, string startTag = "", string groupName = "")
        {
            return "";
        }

        /// <summary>
        /// 单标签写 返回json
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <param name="equipName">设备名</param>
        /// <param name="tagName">标签名</param>
        /// <param name="values">值</param>
        /// <param name="groupName">组名</param>
        /// <returns></returns>
        public virtual string WriteJson(string channelName, string equipName, string tagName, string values, string groupName = "")
        {
            return "";
        }
        #endregion

        #region 获取设备全名
        /// <summary>
        /// 获取设备全名
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <returns></returns>
        public string GetEquipFullName(string channelName,string equipName)
        {
            string channelGui = DbBussiness.Instance.GetChannelGuiByChannelName(channelName);
            string equipFullName = DbBussiness.Instance.GetEquipGuiByChannelGuiAndEquipName(channelGui, equipName);
            return equipFullName;
        }
        #endregion

        #region 获取组全名
        /// <summary>
        /// 获取组全名
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public string GetGroupFullName(string channelName, string equipName,string groupName)
        {
            string channelGui = DbBussiness.Instance.GetChannelGuiByChannelName(channelName);
            string equipFullName = DbBussiness.Instance.GetEquipGuiByChannelGuiAndEquipName(channelGui, equipName);
            string groupFullName = DbBussiness.Instance.GetGroupGuiByEquipChannelGuiAndGroupName(equipFullName, groupName);
            return groupFullName;
        }
        #endregion

        #region 获取标签全名
        /// <summary>
        /// 获取标签全名
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="groupName"></param>
        /// <param name="tagName"></param>
        /// <returns></returns>
        public string GetGroupFullName(string channelName, string equipName, string tagName ,string groupName = "")
        {
            string channelGui = DbBussiness.Instance.GetChannelGuiByChannelName(channelName);
            string equipFullName = DbBussiness.Instance.GetEquipGuiByChannelGuiAndEquipName(channelGui, equipName);
            string tagFullName = "";
            if (groupName == "")
            {
                tagFullName = DbBussiness.Instance.GetTagGuiByGuiAndTagName(equipFullName, tagName);
            }
            else
            {
                string groupFullName = DbBussiness.Instance.GetGroupGuiByEquipChannelGuiAndGroupName(equipFullName, groupName);
                tagFullName = DbBussiness.Instance.GetTagGuiByGuiAndTagName(groupFullName, tagName);
            }
            return tagFullName;
        }
        #endregion
    }
}
